/**
 * commonForm depend vue
 */

function commonFun(elId, urlObj, serverUrl) {
    this.el = elId;
    this.serverUrl = serverUrl;
    this.getDataUrl = urlObj.getDataUrl;
    this.saveUrl = urlObj.saveUrl;
    this.delUrl = urlObj.delUrl;
}
commonFun.prototype = {
    commonFormInit: function() {
        var vm = new Vue({
            el: '#controller',
            data: {
                dataList: [],
                dataObj: {}
            },
            mounted: function() {
                this.init();
            },
            methods: {
                init: function() { // 基础页面初始化

                },
                getData: function() { // 获取页面数据
                    mui.ajax(this.serverUrl + this.getDataUrl, {
                        dateType: 'json',
                        type: 'get',
                        success: function(data) {
                            if (data.code == 200) {
                                vm.dataList = data;
                            } else {
                                mui.toast(data.msg)
                            }
                        },
                        error: function(type, errorThrown) {
                            console.log(errorThrown)
                            mui.toast('服务器异常,请稍后重试!')
                        }
                    })
                },
                delData: function(url, id) { // 删除数据
                    mui.ajax(this.serverUrl + url, {
                        dateType: 'json',
                        type: 'post',
                        data: {
                            del: id
                        },
                        success: function(data) {
                            if (data.code == 200) {
                                vm.getData();
                                mui.toast('删除成功');
                            } else {
                                mui.toast(data.msg)
                            }
                        },
                        error: function(type, errorThrown) {
                            console.log(errorThrown)
                            mui.toast('服务器异常,请稍后重试!')
                        }
                    })
                },
                openWin: function(urlName, id) { // 打开新页面
                    var aId = id;
                    var webview = mui.openWindow({
                        url: urlName,
                        id: urlName,
                        extras: {
                            rid: aId //扩展参数
                        },
                        show: {
                            autoShow: true,
                            aniShow: "auto",
                        },
                        waiting: {
                            autoShow: true,
                            title: '正在加载...'
                        }
                    })
                },
                openWinTitle: function(id, url, type, title) { // 新打开页面 (带导航头)
                    var aId = id,
                        urlId = url,
                        urlName = urlId + ".html",
                        title = title,
                        img = {
                            base64Data: ''
                        };

                    if (type == 'edit') {
                        topTitle = {
                            title: { //标题配置
                                text: title, //标题文字
                            },
                            back: { //左上角返回箭头
                                image: img
                            }
                        }
                    } else {
                        topTitle = {
                            title: { //标题配置
                                text: title, //标题文字
                            },
                            back: { //左上角返回箭头
                                image: img
                            }
                        }
                    };
                    var webview = mui.openWindowWithTitle({
                        url: urlName,
                        id: urlName,
                        extras: {
                            rid: aId //扩展参数
                        },
                        show: {
                            autoShow: true,
                            aniShow: "auto",
                        },
                        waiting: {
                            autoShow: true,
                            title: '正在加载...'
                        }
                    }, topTitle)
                },
                btnFun: function(id, type, url, title, e) { // 按钮事件
                    var rid = id, // 地址 id
                        type = type, // 按钮事件类型
                        title = title, // 新增/编辑到航头内容
                        aUrl = url; // 提交地址/新开页面地址
                    console.log(rid + '---' + aUrl + '---' + type);
                    switch (type) {
                        case 'del': // 删除
                            var msg = $(e.target).attr('sureMsg');
                            mui.confirm('', msg, ['取消', '确认'], function() { // 删除操作询问框 -> 防止误操作
                                vm.delData(this.delUrl, rid);
                            }, 'div')
                            break;
                        case 'edit': // 编辑
                        case 'add': // 新增
                            vm.openWinTitle(rid, aUrl, type, title) // add  rid = null
                            break;
                        default:
                            break;
                    }
                },
                submitFun: function(savePath) {
                    mui.ajax(this.serverUrl + savePath, {
                        dataType: 'json',
                        type: 'post',
                        data: $('#commonForm').serialize(),
                        success: function(res) {
                            console.log(res);
                            mui.plusReady(function() {
                                var current = plus.webview.currentWebview();
                                var currentPar = current.opener(); //查找  父页面
                                mui.fire(currentPar, "pullRefresh"); //触发上级页面的 pageflowrefresh 方法
                                // 关闭页面写在自定义函数下面   可以解决  关闭login后 → 显示的是用户信息页面 → 显示的是首页
                                plus.webview.close(current, 'pop-out');
                            })
                        },
                        error: function(type, errorThrown) {
                            console.log(errorThrown)
                            mui.toast('服务器异常,请稍后重试!')
                        }
                    })
                }
            }

        })
    },
    dateFun: function() { // 日期/时间
        Date.prototype.Format = function(fmt) { //author: meizz
            var o = {
                "M+": this.getMonth() + 1, //月份
                "d+": this.getDate(), //日
                "h+": this.getHours(), //小时
                "m+": this.getMinutes(), //分
                "s+": this.getSeconds(), //秒
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                "S": this.getMilliseconds() //毫秒
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        }

        var date = new Date(),
            dates = date.Format("yyyy/MM/dd"),
            day = date.getDay(),
            week = '';
        switch (day) {
            case 0:
                week = '星期日'
                break;
            case 1:
                week = '星期一'
                break;
            case 2:
                week = '星期二'
                break;
            case 3:
                week = '星期三'
                break;
            case 4:
                week = '星期四'
                break;
            case 5:
                week = '星期五'
                break;
            case 6:
                week = '星期六'
                break;
            default:
                break;
        }
    }
}